/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.ecms.webSetting.service.impl;

import com.ecms.webSetting.domain.TailSetting;
import com.ecms.webSetting.repository.TailSettingRepository;
import com.ecms.webSetting.repository.WebRelateConfigRepository;
import com.ecms.webSetting.service.TailSettingService;
import com.ecms.webSetting.service.dto.TailSettingDto;
import com.ecms.webSetting.service.dto.TailSettingQueryCriteria;
import com.ecms.webSetting.service.mapstruct.TailSettingMapper;
import lombok.RequiredArgsConstructor;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.utils.*;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author ecms
* @date 2021-12-13
**/
@Service
@RequiredArgsConstructor
public class TailSettingServiceImpl implements TailSettingService {
    private final WebRelateConfigRepository webRelateConfigRepository;
    private final TailSettingRepository tailSettingRepository;
    private final TailSettingMapper tailSettingMapper;

    @Override
    public Map<String,Object> queryAll(TailSettingQueryCriteria criteria, Pageable pageable){
        Page<TailSetting> page = tailSettingRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(tailSettingMapper::toDto));
    }

    @Override
    public List<TailSettingDto> queryAll(TailSettingQueryCriteria criteria){
        return tailSettingMapper.toDto(tailSettingRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public List<Map<String,Object>> queryAllGroupByTitle() {

        return tailSettingRepository.groupByTitle();
    }


    @Override
    @Transactional
    public TailSettingDto findById(Long id) {
        TailSetting tailSetting = tailSettingRepository.findById(id).orElseGet(TailSetting::new);
        ValidationUtil.isNull(tailSetting.getId(),"TailSetting","id",id);
        return tailSettingMapper.toDto(tailSetting);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TailSettingDto create(TailSetting resources) {
        int i = tailSettingRepository.countByTitle(resources.getTitle());
        if(i>=4){
            throw new BadRequestException("同一标题下子标题不允许超过四个");
        }
        resources.setCreateBy(SecurityUtils.getCurrentUsername());
        resources.setUpdateBy(SecurityUtils.getCurrentUsername());
        return tailSettingMapper.toDto(tailSettingRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TailSetting resources) {

        int i = tailSettingRepository.countByTitleAndIdNot(resources.getTitle(),resources.getId());
        if(i>=4){
            throw new BadRequestException("同一标题下子标题不允许超过四个");
        }
        resources.setUpdateBy(SecurityUtils.getCurrentUsername());
        TailSetting tailSetting = tailSettingRepository.findById(resources.getId()).orElseGet(TailSetting::new);
        ValidationUtil.isNull( tailSetting.getId(),"TailSetting","id",resources.getId());
        tailSetting.copy(resources);
        tailSettingRepository.save(tailSetting);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {

            tailSettingRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<TailSettingDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TailSettingDto tailSetting : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("标题", tailSetting.getTitle());
            map.put("子标题", tailSetting.getItemTitle());
            map.put("内容", tailSetting.getContext());
            map.put("英文内容", tailSetting.getEnContext());
            map.put("外链", tailSetting.getHrefLink());
            map.put("创建者", tailSetting.getCreateBy());
            map.put("更新者", tailSetting.getUpdateBy());
            map.put("创建日期", tailSetting.getCreateTime());
            map.put("更新时间", tailSetting.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }


}